<template>
  <div class="app-container home">
    <div class="phone">
      <div class="ststus">
        <div class="item">
          {{shiOrFen}}
        </div>
        <div class="item">

        </div>
        <div class="item">
          <img src="../assets/images/cell.png">
        </div>
      </div>
      <div class="cont">
        <template v-if="showIndex == 1">
          <div @click="handleClick"
               class="sao">
            <img src="../assets/images/sao.png">
            扫一扫
          </div>
        </template>
        <template v-if="showIndex == 2">
          <div class="pay">
            <div class="pay-form">
              <el-form ref="machineData"
                       :model="machineData"
                       label-width="50px">
                <el-form-item label="编号"
                              prop="machineNo">
                  {{machineData.machineNo}}
                </el-form-item>
                <el-form-item label="模式"
                              prop="laundryPattern">
                  <el-select v-model="machineData.laundryPattern"
                             placeholder="请选择模式"
                             @change="onChangePattem">
                    <el-option v-for="item in pattemList"
                               :key="item.id"
                               :label="item.laundryPattern"
                               :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-form>
            </div>
            <div class="pay-button">
              <div class="item">
                金额：{{machineData.pay||0}}元
              </div>
              <div class="item">
                <el-button type="success"
                           @click="onClickPay">支付</el-button>
              </div>
            </div>
          </div>
        </template>
      </div>
    </div>
    <input type="file"
           ref="input"
           @change="handleImageUpload"
           class="input-style" />
  </div>
</template>

<script>
import { update } from "@/api/system/machine";
import { listPattern } from "@/api/system/pattern";
import jsQR from 'jsqr';
export default {
  name: 'Phone',
  data () {
    return {
      machineData: {},
      showIndex: 1,
      nowDate: '',
      shiOrFen: '',
      pattemList: []
    }
  },
  created () {
    this.nowDate = new Date()
    let hour = this.nowDate.getHours()
    let minutes = this.nowDate.getMinutes()
    this.shiOrFen = hour + ':' + minutes
    setInterval(() => {
      let hour = this.nowDate.getHours()
      let minutes = this.nowDate.getMinutes()
      this.shiOrFen = hour + ':' + minutes
    }, 1000);
    this.getPattermList()
  },
  methods: {
    getPattermList () {
      listPattern().then(res => {
        this.pattemList = res.rows
      })
    },
    onChangePattem (e) {
      let obj = this.pattemList.find(item => item.id == e)
      if (obj.id) {
        this.machineData.pay = obj.amount
        this.machineData.laundryPattern = obj.laundryPattern
      }
    },
    handleClick () {
      if (this.disabled) return
      this.$refs.input.click()
    },
    onClickPay () {
      let params = {
        "machineNo": this.machineData.machineNo,
        "laundryPattern": this.machineData.laundryPattern,
        "id": this.machineData.id
      }
      update(params).then(res => {
        this.$modal.msgSuccess('支付成功')
        this.showIndex = 1
        this.machineData = {}
      })
    },
    handleImageUpload (event) {
      const file = event.target.files[0];

      if (file) {
        const reader = new FileReader();

        reader.onload = (e) => {
          const imageData = new Image();
          imageData.onload = () => {
            const canvas = document.createElement('canvas');
            const context = canvas.getContext('2d');

            canvas.width = imageData.width;
            canvas.height = imageData.height;
            context.drawImage(imageData, 0, 0, imageData.width, imageData.height);

            const imageDataArray = context.getImageData(0, 0, imageData.width, imageData.height).data;
            const code = jsQR(imageDataArray, imageData.width, imageData.height);

            if (code) {
              const qrCodeData = code.data;
              console.log('QR Code Data:', qrCodeData);
              console.log(JSON.parse(qrCodeData));
              if (qrCodeData) {
                let data = JSON.parse(qrCodeData)
                if (data.id && data.machineNo) {
                  this.machineData = data
                  this.showIndex = 2
                }
              }
              // You can now use qrCodeData as needed
            } else {
              console.error('QR Code not found');
            }
          };

          imageData.src = e.target.result;
        };

        reader.readAsDataURL(file);
      }
    },
  },
}
</script>

<style scoped lang="scss">
.input-style {
  display: none;
}
.phone {
  width: 375px;
  height: 700px;
  border: 1px solid #000;
  border-radius: 30px;
  display: flex;
  flex-direction: column;
}
.ststus {
  display: flex;
  padding: 10px 20px 0;
  .item {
    flex: 1;
    img {
      width: 20px;
      height: 10x;
    }
  }
  .item:last-child {
    display: flex;
    justify-content: flex-end;
  }
}
.cont {
  flex: 1;
}
.sao {
  width: 60%;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 20px auto;
  img {
    width: 100px;
  }
}
.pay {
  display: flex;
  flex-direction: column;
  height: 100%;
  .pay-form {
    flex: 1;
    padding: 0 20px;
  }
  .pay-button {
    height: 60px;
    border-top: 1px solid #252525;
    display: flex;
    align-items: center;
    .item {
      flex: 1;
      text-align: center;
    }
  }
}
</style>

